declare -A types_fr
types_fr=(["-"]="RÉGULIER" [d]="RÉPERTOIRE" [l]="LIEN SYMBOLIQUE" [p]="TUBE NOMMÉ" [s]="SOCKET" [c]="SPÉCIAL MODE CARACT" [b]="SPÉCIAL MODE BLOC")

function mkidx {
#has to receive the output of a "ls -ilrt"
	local f
	read f #total
	if [ "$f" == "total 0" ]; then
		echo Aucun lien à part peut-être des répertoires
		return
	else
		read f
		while [ -n "$f" ]; do
			local ls=($f)
			local p=${ls[1]} #mostly permissions
			local rt=${p:0:1} #raw type
			if [ "$rt" != d ]; then
				local n=${ls[9]} #link name
				local t=${types_fr[$rt]} #type in French
				local i=${ls[0]} #inode number
				local sz=${ls[5]} #size
				local s #synonym or not
				if [ ${ls[2]} -gt 1 ]; then
					s="\tSYNONYME"
				fi
				echo -e "$n\t$sz\t$t\t$i$s"
			fi
			read f
		done
	fi
}


function dupli {
	echo $1 to $2
	local f
	mkdir "$2"
	abs2=`cd "$2"; pwd`
	pushd "$1" >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		ls -ilrt 2>/dev/null|mkidx >"$abs2/index.txt" 2>&1
		if [ ${PIPESTATUS[0]} -eq 0 ]; then
			#TODO do I have to copy hidden files too?
			#TODO if so, avoid infinite recursion (. and ..)
			#TODO maybe using .[^.]*
			for f in *; do
				if [ -d "$f" ]; then
					dupli "$f" "$abs2/$f"
				fi
			done
		else
			echo Can\'t read links >"$abs2/index.txt"
		fi
		popd>/dev/null
	else
		echo Can\'t change directory... >"$abs2/index.txt"
		return
	fi
}


renice -n 10 $$ >/dev/null
dupli "$1" "$2"
